/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.jmx.mbeanserver.JmxMBeanServer;
import com.util.FormUtil;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.Conection;
import model.Dokument;
import model.Dosen;
import model.Surat;

public class FrameUtamaDosen extends JFrame {

    private JMenuBar menuBar;
    private JMenu periksa;
    private JMenuItem surat, dokumen;
    private Dosen dsn;
    private JLabel gambar;

    public FrameUtamaDosen(Dosen dsn) {
        this.setSize(516, 550);
        FormUtil.centerWindow(this);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setTitle(" Frame DOSEN ");
        this.dsn = dsn;

        menuBar = new JMenuBar();
        setJMenuBar(menuBar);


        periksa = new JMenu(" PERIKSA ");
        menuBar.add(periksa);

        surat = new JMenuItem(" SURAT ");
        periksa.add(surat);

        surat.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tampilSuratAction();
                if (surat.isEmpty() == false) {
                    JFrame fr = new JFrame();
                    PanelLihatTabelSurat pan = new PanelLihatTabelSurat(surat);
                    fr.getContentPane().add(pan);
                    FormUtil.setMaximize(fr);
                    fr.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Surat untuk Anda tidak ada");

                }
            }
            private ArrayList<Surat> surat;

            private void tampilSuratAction() {
                surat = new ArrayList<Surat>();
                Connection con = Conection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "sisil");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(FrameUtamaDosen.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from SURAT WHERE PENERIMA IN ( SELECT DATA FROM CACHE )";
                ResultSet res = null;
                try {
                    res = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(FrameUtamaDosen.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (res.next()) {
                        Surat sur = new Surat();
                        sur.setNomorSurat(res.getString("NO_SURAT"));
                        sur.setPerihal(res.getString("PERIHAL"));
                        sur.setAsalSurat(res.getString("ASAL_SURAT"));
                        sur.setPenerimaSurat(res.getString("PENERIMA"));
                        sur.setLetakSurat(res.getString("LETAK_SURAT"));
                        sur.setPetugasPenerimaSurat(res.getString("PETUGAS_PENERIMA_SURAT"));
                        sur.setTanggal(res.getString("TANGGAL"));
                        surat.add(sur);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(FrameUtamaDosen.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        });

        dokumen = new JMenuItem("DOCUMENT");
        periksa.add(dokumen);

        dokumen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tampilDocumentAction();
                if (dokument.isEmpty() == false) {
                    JFrame fr = new JFrame();
                    PanelLihatTabelDokument pan = new PanelLihatTabelDokument(dokument);
                    fr.getContentPane().add(pan);
                    FormUtil.setMaximize(fr);
                    fr.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Dokumen untuk Anda tidak ada");
                }
            }
            private ArrayList<Dokument> dokument;

            private void tampilDocumentAction() {
                dokument = new ArrayList<>();
                Connection con = Conection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "sisil");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(FrameUtamaDosen.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from DOKUMEN WHERE PENERIMA IN ( SELECT DATA FROM CACHE )";
                ResultSet res = null;
                try {
                    res = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(FrameUtamaDosen.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (res.next()) {
                        Dokument doc = new Dokument();
                        doc.setNoDokument(res.getString("NO_DOKUMEN"));
                        doc.setPerihal(res.getString("PERIHAL"));
                        doc.setAsalDokument(res.getString("ASAL_DOKUMEN"));
                        doc.setPenerimaDokument(res.getString("PENERIMA"));
                        doc.setLetakDokument(res.getString("LETAK_DOKUMEN"));
                        doc.setPetugasPenerimaDokument(res.getString("PETUGAS_PENERIMA_DOKUMEN"));
                        doc.setTanggal(res.getString("TANGGAL"));
                        dokument.add(doc);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(FrameUtamaDosen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        gambar = new JLabel(new ImageIcon("gambar/welcome.jpg"));
        gambar.setSize(500, 500);
        gambar.setLocation(0, 0);
        gambar.setVisible(true);
        this.getContentPane().add(gambar);
        this.setVisible(true);
    }
;
}
